package com.wuhunyu.controller;

import com.alibaba.fastjson.JSONObject;
import com.wuhunyu.commonutils.result.CommonResult;
import com.wuhunyu.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/acl/index")
//@CrossOrigin
public class IndexController {

    @Autowired
    private IndexService indexService;

    /**
     * 根据token获取用户信息
     */
    @GetMapping("/info")
    public CommonResult info(){
        //获取当前登录用户用户名
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        Map<String, Object> userInfo = indexService.getUserInfo(username);
        return CommonResult.success(userInfo);
    }

    /**
     * 获取菜单
     * @return
     */
    @GetMapping("/menu")
    public CommonResult getMenu(){
        //获取当前登录用户用户名
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        List<JSONObject> permissionList = indexService.getMenu(username);
        Map<String, Object> map = new HashMap<>(16);
        map.put("permissionList", permissionList);
        return CommonResult.success("获取菜单信息成功", map);
    }

    @PostMapping("/logout")
    public CommonResult logout(){
        return CommonResult.success("登录成功");
    }

}
